/**
 *    Copyright (C) 2008, 2009  Victor Smirnov <aist11@gmail.com>
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License along
 *    with this program; if not, write to the Free Software Foundation, Inc.,
 *    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 *    See LICENSE.exceptions in the root folder of this project for possible
 *    exceptions
 *
 */

package ru.teleology.cubefs.editor.hex;

import java.awt.GridLayout;
import java.util.HashSet;
import java.util.Set;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.EventListenerList;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;

public class HexField extends JTextField {
	private static final long serialVersionUID = -1705513006898698696L;
	
	private static final Set<Character> ACCEPTED = new HashSet<Character>(); 
	
	static {
		ACCEPTED.add('0');
		ACCEPTED.add('1');
		ACCEPTED.add('2');
		ACCEPTED.add('3');
		ACCEPTED.add('4');
		ACCEPTED.add('5');
		ACCEPTED.add('6');
		ACCEPTED.add('7');
		ACCEPTED.add('8');
		ACCEPTED.add('9');
		ACCEPTED.add('A');
		ACCEPTED.add('B');
		ACCEPTED.add('C');
		ACCEPTED.add('D');
		ACCEPTED.add('E');
		ACCEPTED.add('F');
		
		ACCEPTED.add('a');
		ACCEPTED.add('b');
		ACCEPTED.add('c');
		ACCEPTED.add('d');
		ACCEPTED.add('e');
		ACCEPTED.add('f');
	}
	
	
	private EventListenerList listenerList = new EventListenerList();
	
	public HexField() {
		setColumns(2);
	}
	
	public HexField(int columns) {
		setColumns(columns);
	}
	
	protected Document createDefaultModel() {
		return new HexDocument();
	}

	private class HexDocument extends PlainDocument {

		public void insertString(int offs, String str, AttributeSet a) 
		throws BadLocationException {
			if (str == null) {
				return;
			}
			
			int columns = getColumns();
			
			if (str.length() + getLength() > columns) {
				fireActionPerformed();
				return;
			}
			
			StringBuilder sb = new StringBuilder();
			
			for (int c = 0; c < str.length(); c++) {
				if (ACCEPTED.contains(str.charAt(c))) {
					sb.append(str.charAt(c));
				}
			}
			
			if (sb.length() > 0) {
				super.insertString(offs, sb.toString(), a);
			}
			
			if (getLength() == columns) {
				fireActionPerformed();
			}
		}
	}
	
	@Override
	public void setText(String t) {
		super.setText("");
	}
	
	
	public static void main(String[] args) {
		JFrame frame = new JFrame("HexField test");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel();
		
		panel.setLayout(new GridLayout());
		
		panel.add(new HexField());
		
		frame.add(panel);
		
		frame.setSize(200,200);
		frame.setVisible(true);
		
	}
}
